﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICAlIn : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        public string key = "";
        public string FBrNo = "";
        private string ID;

        public ICAlIn()
        {
            InitializeComponent();
        }

        public ICAlIn(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }
        
        private void ICAlIn_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }
        //获取数据
        private void BinData()
        {
            //获取库位名称states:0扫描库位的条码与调拨单中条码不一致，states:1扫描库位的条码与调拨单中条码一致，states:3 扫描的库位为空,错误
            DataSet ds = bjs.GetICAllocation("3");
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICAllocation";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "调拨单号";
            cl2.MappingName = "FBrNo";
            cl2.Width = 120;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "开单时间";
            cl3.MappingName = "Fdate";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "开单人";
            cl4.MappingName = "FBillerID";
            cl4.Width = 50;
            dts.GridColumnStyles.Add(cl4);

            dataGrid1.TableStyles.Add(dts);

        }
        //选择记录
        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FBrNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
        }
        //返回
        private void bt_back_Click(object sender, EventArgs e)
        {
            Main main = new Main(this.ID);
            main.Show();
            this.Hide();
        }
        //刷新
        private void bt_refresh_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }
        //明细
        private void bt_detail_Click(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            int num = 0;
            ICAlInDetail ica = new ICAlInDetail(FBrNo,this.ID,out num );
            if (num == 0)
            {
                MessageBox.Show("该单据的包装码已经全部提交，请选择完成订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.Show();
                return;
            }
            else {
                ica.Show();
                this.Hide();
            }
           
        }
        //完成
        private void bt_finish_Click(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            DialogResult dr = MessageBox.Show("是否完成订单： " + FBrNo + " ？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

            if (dr == DialogResult.Yes)
            {
                bool lb = false;
                //int num = bjs.filishDiaobojd(FBrNo);
                //if (num == 0)
                //{
                //    MessageBox.Show("调拨入库同步到金蝶失败，请联系开发人员！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                //    return;
                //}
                //else
                //{
                    lb = bjs.FinishICA(FBrNo);
                    if (lb)
                    {
                        DataGridStyle();
                        BinData();
                    }
                    else
                    {
                        MessageBox.Show("完成调拨单失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                //}

            }
        }
    }
}